package view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

import java.awt.Font;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import algorithm.RoundRobin;
import pojo.ProcessPojo;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

public class RRview {
	private JFrame frame;
	private JTable table;
	private RoundRobin rr;//引入时间轮转
	private Vector data;
	private Vector Title;
	private DefaultTableModel model;
	private JTextField timel;
	private JTextField pcbsum;
	private JButton button_1;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RRview window = new RRview();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	public RRview() {
		initialize();
	}


	private void initialize() {
		frame = new JFrame();
		frame.setVisible(true);
		frame.setBounds(100, 100, 861, 526);
		frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("时间片轮转模拟算法");
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		label.setBounds(316, 0, 191, 60);
		frame.getContentPane().add(label);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(0, 103, 843, 376);
		frame.getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setRowHeight(18);
		table.setEnabled(false);
		table.setForeground(Color.WHITE);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		table.setBackground(Color.DARK_GRAY);	
		scrollPane.setViewportView(table);//让表格在scrollPane可见
		
		JButton button = new JButton("初始化就绪队列");
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				initQueue();			
			}
			
		});
		button.setBounds(369, 60, 151, 33);
		frame.getContentPane().add(button);
		
		button_1 = new JButton("开始模拟");
		button_1.setEnabled(false);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				start();
			}

		});
		button_1.setBounds(565, 60, 151, 33);
		frame.getContentPane().add(button_1);
		
		timel = new JTextField();
		timel.setBounds(78, 66, 86, 24);
		frame.getContentPane().add(timel);
		timel.setColumns(10);
		
		JTextArea textArea = new JTextArea();
		textArea.setText("时间片");
		textArea.setBounds(14, 66, 50, 24);
		frame.getContentPane().add(textArea);
		
		JTextArea textArea_1 = new JTextArea();
		textArea_1.setText("进程数");
		textArea_1.setBounds(191, 66, 50, 24);
		frame.getContentPane().add(textArea_1);
		
		pcbsum = new JTextField();
		pcbsum.setColumns(10);
		pcbsum.setBounds(255, 66, 86, 24);
		frame.getContentPane().add(pcbsum);
	}
	private void start() {
		//开始模拟
		new Thread(()->{
			HashSet<ProcessPojo>runSet=rr.getRunSet();
			LinkedList<ProcessPojo>readyQueue=rr.getReadyQueue();
			while(!readyQueue.isEmpty()) {
				this.rr.runMock();
				readyQueue=rr.getReadyQueue();
				runSet=rr.getRunSet();
				for(ProcessPojo pcb:runSet) {
					Vector rows=new Vector<>();
					rows.add(pcb.getName());rows.add(pcb.getState());rows.add(pcb.getNeedTime());rows.add(pcb.getCpuTime());rows.add(pcb.getTimeSlice());
					data.add(rows);
					model.setDataVector(data, Title);
					model.fireTableDataChanged();
					table=new JTable(model);
					table.validate();
					table.updateUI();
				}
			}
			System.out.println("模拟结束.");
		}).start();
		
	}


	private void initQueue() {
		//初始化表格标题
		this.Title=new Vector<>();
		Title.add("进程名");
		Title.add("当前状态");
		Title.add("需要时间");
		Title.add("cpu占用时间");
		Title.add("时间片大小");	
		//初始化就绪队列的数据
		this.rr=new RoundRobin();
		String strt=timel.getText();
		String strp=pcbsum.getText();
		if(strt.length()==0||strp.length()==0) {
			//输入格式错误
			new WarnView();
		}
		else {
			int time=Integer.parseInt(strt);//获取输入框的值
			int pcbcnt=Integer.parseInt(strp);
			
			this.rr.setTimeSlice(time);//设置时间片为3
			this.rr.init(pcbcnt);//设置进程数为5
			LinkedList<ProcessPojo>queue=this.rr.getReadyQueue();
			this.data=new Vector<>();
			for(ProcessPojo pcb:queue) {
				Vector rows=new Vector<>();
				rows.add("初始:"+pcb.getName());rows.add("初始:"+pcb.getState());rows.add("初始:"+pcb.getNeedTime());rows.add("初始:"+pcb.getCpuTime());rows.add("初始:"+pcb.getTimeSlice());
				data.add(rows);
			}
			model = new DefaultTableModel(data,Title);
			table.setModel(model);
			System.out.println("就绪队列初始化成功");
			button_1.setEnabled(true);
		}
		
	}
}
